$(document).ready(function() {
    // set up the news accordion on the lower page
    
    $("li a:contains(Statistics)").addClass('current');

    //$("select#category").change(get_complaints);
    $("p#change").hide();
    
    //  $("p.gear a").mouseover(function(){alert($(this).html());});
    $("button").button({
        icons: {
            primary: "ui-icon-gear"
        }
    }).click(generate_stat);
    $("select#month").change(change);
    $("select#year").change(change);

   
});
var ticks = ['New Connection', 'Shift Connection', 'Complain'];
var months=new Array("none","January","February","March","April","May","June","July","August","Septemabar","Octobar","Novembar","Decembar");
var exchange,code,year,month;
function change()
{
    month = $("select#month option:selected").get(0).value;
    year=$("select#year option:selected").text();
    var data="code="+code+"&month="+month+"&year="+year;
    $.ajax({
        type: 'POST',
        url: base_url() + 'statistics/get_data_ajax',
        data: data,
        datatype: "json",
        success : function (result) {
            var info=$.parseJSON(result);
            var s1 = new Array(info.received, 8, 10);
            var s2 = new Array(info.rejected, 4, 2);
            var s3 = new Array(info.completed, 2, 5);
            var html="";
            for(var i=info.month ; i>0 ;i--)
            {
                html+="<option value="+i+">"+ months[i]+"</option>"   ;
             
            }
            $("select#month").html(html);
            $("select#month").val(month);

          
            var header=exchange+" Exchange "+months[month]+" "+year;
            $("#head").text(header);
            draw_chart(s1,s2,s3);
            
        }
    }); 
}
function change_year()
{
    year=$("select#year option:selected").text();
}
function generate_stat()
{
    var obj = $("select#exchange option:selected").get(0);
    code=obj.value;
    exchange=obj.innerHTML.toString();
    var data="code="+code;
    $.ajax({
        type: 'POST',
        url: base_url() + 'statistics/get_data_ajax',
        data: data,
        datatype: "json",
        success : function (result) {
            var info=$.parseJSON(result);
            var s1 = new Array(info.received, 8, 10);
            var s2 = new Array(info.rejected, 4, 2);
            var s3 = new Array(info.completed, 2, 5);
            var html="";
            for(var i=info.month ; i>0 ;i--)
            {
                html+="<option value="+i+">"+ months[i]+"</option>"   ;
             
            }
            $("select#month").html(html);
            month=info.month;
            year=info.year;
      
            var header=exchange+" Exchange "+months[month]+" "+year;
            $("#head").text(header);
            draw_chart(s1,s2,s3);
            $("p#change").show();
            
        }
    }); 

}

function get_data_ajax(data)
{
  
    $.ajax({
        type: 'POST',
        url: base_url() + 'statistics/get_data_ajax',
        data: data,
        datatype: "json",
        success : function (result) {
            info=$.parseJSON(result);
        }
    }); 

}
function draw_chart(s1,s2,s3)
{

    plot1 = $.jqplot('chart1', [s1, s2, s3], {
        seriesDefaults:{
            renderer:$.jqplot.BarRenderer,
            rendererOptions: {
                fillToZero: true
            }
        },
        series:[
        {
            label:'Received'
        },

        {
            label:'Rejected'
        },

        {
            label:'Completed'
        }
        ],
        legend: {
            show: true,
            placement: 'outsideGrid'
        },
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: ticks
            },
            yaxis: {
                autoscale: true,
                tickOptions: {
                    formatString: '%d'
                }
            }
        }
    }).replot();
}